package com.模板模式.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.模板模式.ExcelSelectUtil;
import com.模板模式.ExcelUtiles;
import com.模板模式.ExportEnum;
import com.模板模式.pojo.Goods;
import com.模板模式.pojo.Order;
import com.模板模式.pojo.User;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public abstract class ExportSuperService<T> {

    public static Map<String,String> map=new HashMap<>();
    static {
        map.put("京东物流","jd");
        map.put("申通快递","shentong");
        map.put("顺丰速运","shunfeng");
        map.put("申通快运","stoe56");
        map.put("圆通速递","yuantong");
    }
    /**
     * 获取标题，导出excel最顶部的标题是不一样的
     */
    public String getTitle(ExportEnum exportEnum){
        return exportEnum.getTitle();
    }

    /**
     * 下拉选,可以根据ExportEnum的类型选择相应的下拉选值
     */
    public Set<String>  selectList(ExportEnum exportEnum){
        Set<String> keys = map.keySet();
        return keys;
    }
    /**
     * 从数据库中获取数据，从数据库中获取到的数据由子类来实现
     */
    public abstract List<Object> findList();

    /**
     * 执行导出
     */
    public void export(List<T> list,ExportEnum exportEnum, HttpServletResponse response){
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        ExportParams exportParams = new ExportParams(getTitle(exportEnum), getTitle(exportEnum));
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,exportEnum.getClzz(),list);
        if (exportEnum.toString().equals("ORDER")){
            Set<String> set=selectList(exportEnum);
            ExcelSelectUtil.selectList(workbook, 3, Integer.MAX_VALUE, 3, 3, set.toArray(new String[set.size()]), 1);
        }
        ExcelUtiles.downLoadExcel(getTitle(exportEnum)+".XLS",response,workbook);
    }


}
